package com.edu.gcu.manager.controller;

import com.edu.gcu.manager.service.IFileService;
import com.edu.gcu.manager.service.IIntegralService;
import com.edu.gcu.system.common.Const;
import com.edu.gcu.system.common.ResponseCode;
import com.edu.gcu.system.common.ServerResponse;
import com.edu.gcu.system.pojo.Admin;
import com.edu.gcu.system.pojo.Gift;
import com.edu.gcu.system.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author polo
 * @version V1.0
 * @description: 后台兑换商品控制器
 * @date 2018-02-20-12:09
 */

@Controller
@RequestMapping("/manage/gift")
public class GiftManageController {


    private static Logger logger = LoggerFactory.getLogger(GiftManageController.class);

    @Autowired
    private IIntegralService iIntegralService;

    @Autowired
    private IFileService iFileService;

    /**
     * @description 添加兑换商品
     * @author polo
     */
    @ResponseBody
    @RequestMapping("add.do")
    public ServerResponse add(Gift gift, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return iIntegralService.add(currentAdmin.getId(), gift);
    }

    /**
     * @description 删除兑换商品
     * @author polo
     */
    @ResponseBody
    @RequestMapping("delete.do")
    public ServerResponse delete(String id, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return iIntegralService.delete(currentAdmin.getId(), id);
    }

    /**
     * @description 删除多个兑换商品
     * @author polo
     */
    @ResponseBody
    @RequestMapping("deleteMore.do")
    public ServerResponse deleteMore(String ids, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return iIntegralService.delete(currentAdmin.getId(), ids);
    }

    /**
     * @description 修改兑换商品
     * @author polo
     */
    @ResponseBody
    @RequestMapping("update.do")
    public ServerResponse update(Gift gift, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return iIntegralService.update(currentAdmin.getId(), gift);
    }

    /**
     * @description 更改兑换商品状态
     * @author polo
     */
    @ResponseBody
    @RequestMapping("change.do")
    public ServerResponse change(String id, Integer status, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return iIntegralService.change(currentAdmin.getId(), id, status);
    }

    /**
     * @description 返回单个兑换商品
     * @author polo
     */
    @ResponseBody
    @RequestMapping("detail.do")
    public ServerResponse detail(String id, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return iIntegralService.detail(id);
    }

    /**
     * @description 兑换商品列表
     * @author polo
     */
    @ResponseBody
    @RequestMapping("list.do")
    public ServerResponse list(@RequestParam(value = "name", defaultValue = "") String name,
                               @RequestParam(value = "status", defaultValue = "") Integer status,
                               @RequestParam(value = "orderBy", required = false) String orderBy,
                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return iIntegralService.list(name, status, orderBy, pageNum, pageSize);
    }

    /**
     * @description 图片上传
     * @author polo
     */
    @ResponseBody
    @RequestMapping("upload.do")
    public ServerResponse upload(MultipartFile file,
                                 HttpSession session, HttpServletRequest request) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        String path = request.getSession().getServletContext().getRealPath("upload");
        String fileName = iFileService.upload(file, path);
        Map<String, Object> resultMap = new HashMap<String, Object>(16);
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix", "http://img.polott.top/") + "img/" +
                fileName;
        resultMap.put("uri", fileName);
        resultMap.put("url", url);
        return ServerResponse.createBySuccess(resultMap);
    }

    /**
     * @description 图片删除
     * @author polo
     */
    @ResponseBody
    @RequestMapping("deleteImg.do")
    public ServerResponse deleteImg(String file,
                                    HttpSession session, HttpServletRequest request) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN
                    .getDesc());
        }
        return iFileService.deleteFile(file);
    }
}
